<?php

/**
 * event actions.
 *
 * @package    bolao-aabb
 * @subpackage event
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class eventActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    // Get month and year parameters
    $month = $request->getParameter('month', date('m'));
    $year  = $request->getParameter('year',  date('Y'));

    // Get month events
    $month_events = BolaoEventPeer::getMonthEvents($month, $year);

    $this->month = $month;
    $this->year  = $year;
    $this->month_events  = $month_events;
    $this->events_by_day = BolaoEventPeer::organizeEventsByDay($month_events);

    if ($request->isXmlHttpRequest())
    {
      $day = $request->getParameter('day');
      $date = $year.'-'.$month.'-'.$day;

      if (count($this->events_by_day[$date]) > 1)
      {
        $pager = new sfPropelPager('BolaoEvent', 1);
        $pager->setCriteria(BolaoEventPeer::getCriteriaForEventsAt($year, $month, $day));
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();

        return $this->renderPartial('event/detailsbyday', array(
          'pager' => $pager,
          'day' => $day,
          'month' => $this->month,
          'year' => $this->year
        ));
      }
      else
        return $this->renderPartial('event/details', array('event' => current($this->events_by_day[$date])));
    }
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->event = $this->getRoute()->getObject();

    if ($request->isXmlHttpRequest())
    {
      return $this->renderPartial('event/details', array('event' => $this->event));
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $bolao_event = new BolaoEvent();
    $bolao_event->setCreatorId($this->getUser()->getProfile()->getUserId());
    $this->form = new BolaoEventForm($bolao_event);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new BolaoEventForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $bolao_event = $this->getRoute()->getObject();

    $this->form = new BolaoEventForm($bolao_event);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));

    $bolao_event = $this->getRoute()->getObject();

    $this->form = new BolaoEventForm($bolao_event);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $bolao_event = $this->getRoute()->getObject();

    $bolao_event->delete();

    $this->redirect('event/index?month='.$bolao_event->getStartDate('m').'&year='.$bolao_event->getStartDate('Y'));
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $bolao_event = $form->save();

      if ($form['send_email']->getValue())
        $this->redirect('@send_email?type=event&id='.$bolao_event->getId());
      else
        $this->redirect('event/index?month='.$bolao_event->getStartDate('m').'&year='.$bolao_event->getStartDate('Y'));
    }
  }
}
